#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int mod=998244353,inf=0x3f3f3f3f3f3f3f3f;
const int N=2e2+10,M=2e5+10;
int n,a[N];
int f[N][N][N];
inline void getmax(int &x,int y){x=max(x,y);}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	freopen("shung.in","r",stdin);
	freopen("shung.out","w",stdout);
	cin >> n;
	for ( int i = 1 ; i <= n ; i++ )cin >> a[i];
	for ( int i = 0 ; i <= n ; i++ )
	{
		for ( int j = 0 ; j <= n ; j++ )
		{
			for ( int k = 1 ; k <= n+1 ; k++ )
			{
				if(j<a[i+1])getmax(f[i+1][a[i+1]][k],f[i][j][k]+1);
				else getmax(f[i+1][j][k],f[i][j][k]);
				if(a[i+1]<k)getmax(f[i+1][j][a[i+1]],f[i][j][k]+1);
				else getmax(f[i+1][j][k],f[i][j][k]);
//				cerr << f[i][j][k] << " ";
			}
//			cerr << endl;
		}
//		cerr << endl;
	}
	int ans=0;
	for ( int i = 0 ; i <= n ; i++ )
		for ( int j = 1 ; j <= n+1 ; j++ )
			getmax(ans,f[n][i][j]);
	cout << ans;
	return 0;
}
/*
10
3 10 4 1 2 7 9 5 6 8
*/

